<template>
  <div class='container'>
    <el-card>
      <el-header >
        <div class="left">
          <span>关键字</span>
          <el-input v-model="keyword" type="text" placeholder="根据编号搜索"></el-input>
        </div>
        <div class="right">
          <el-button @click="clear" size="small">清除</el-button>
          <el-button @click="search" size="small" type="primary">搜索</el-button>
        </div>
      </el-header>
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        :closable="false"
        style="margin-bottom:20px"
        show-icon>
      </el-alert>

    <el-table :data="randomTableData" style="width: 100%" :header-cell-style="{background:'#fafafa','border-bottom':'3px solid #e8e8e8'}">
      <el-table-column prop="id" label="编号" width="220"> </el-table-column>
      <el-table-column label="题型" width="80">
        <template v-slot="scope">
          {{scope.row.questionType | filterType}}
        </template>
         </el-table-column>
      <el-table-column prop="date" label="题目编号" width="220">
        <template v-slot="scope">
          <el-link :underline="false" @click="dialogShow(item.id)" v-for="(item,index) in scope.row.questionIDs" :key="index" type="primary">{{item.number}}</el-link>
        </template>
       </el-table-column>
      <el-table-column prop="addTime" label="录入时间" width="180">
        <template >
          {{new Date()|addDate}}
        </template>
         </el-table-column>
      <el-table-column prop="totalSeconds" label="答题时间(s)" > </el-table-column>
      <el-table-column prop="accuracyRate" label="正确率(%)" > </el-table-column>
      <el-table-column prop="userName" label="录入人" > </el-table-column>
      <el-table-column label="操作" width="80">
        <template v-slot="scope">
          <el-button @click="del(scope.row.id)" type="danger" icon="el-icon-delete" class="is-plain" circle></el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="q.page"
      :page-sizes="[20, 30, 40]"
      :page-size="q.pagesize"
      layout="prev, pager, next,sizes, jumper"
      :total="total"
      >
    </el-pagination>
    </el-card>
    <el-dialog
      @closed="this.row={}"
      title="题目预览"
      :visible.sync="dialogVisible"
      width="50%">
      <questionsPpreview
      v-if="dialogVisible"
      :questionDetails="row"
      ></questionsPpreview>
    </el-dialog>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '../../api/hmmm/questions'
import { questionType } from '../../api/hmmm/constants'
import questionsPpreview from '../components/questions-preview'
export default {
  components: { questionsPpreview },

  data () {
    return {
      randomTableData: [],
      q: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      keyword: '',
      total: 0,
      dialogVisible: false,
      row: {}
    }
  },
  filters: {
    filterType: (value) => {
      let i = ''
      questionType.forEach(item => {
        if (item.value + '' === value) {
          i = item.label
        }
      })
      return i
    }
  },
  created () {
    this.getRandomsList()
  },
  methods: {
    async getRandomsList () {
      const res = await randoms(this.q)
      this.randomTableData = res.data.items
      this.total = res.data.counts
    },
    handleSizeChange (val) {
      this.q.pagesize = val
      this.getRandomsList()
    },
    handleCurrentChange (val) {
      this.q.page = val
      this.getRandomsList()
    },
    // 删除
    async del (id) {
      const result = await this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', { type: 'warning' }).catch(e => e)
      if (result === 'cancel') return
      const res = await removeRandoms({ id })
      if (res.status === 200) {
        if (this.randomTableData.length === 1 && this.q.page > 1) {
          this.q.page--
        }
        this.$message.success('删除成功')
        this.getRandomsList()
      }
    },
    // 获取数据详情展示
    async dialogShow (id) {
      this.dialogVisible = true
      const res = await detail({ id })
      this.row = res.data
    },
    // 搜索
    search () {
      this.q.keyword = this.keyword
      this.getRandomsList()
    },
    // 清除
    clear () {
      this.keyword = ''
      this.q.keyword = ''
      this.getRandomsList()
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 10px;
  .el-header {
    display: flex;
    justify-content: space-between;
    padding-right: 0px;
    .left {
      display: flex;
      align-items: center;
      span {
        width: 80px;
        font-weight: 700;
        color: #606266;
      }
      ::v-deep.el-input__inner {
        width: 300px;
      }
    }
    .right {
      display: flex;
      align-items: center;
    }
  }
  .el-pagination {
    text-align: right;
    margin-top: 10px;
  }
  .el-button--danger.is-plain {
    color: #f56c6c;
    background: #fef0f0;
    border-color: #fbc4c4;
  }
  .el-button--danger.is-plain:hover {
    background: #f56c6c;
    border-color: #f56c6c;
    color: #fff;
  }
}
</style>
